package com.mxfcode.back.controller;

import com.mxfcode.annotation.LoginRequired;
import com.mxfcode.back.dto.PermissionDTO;
import com.mxfcode.back.dto.UserSearchDTO;
import com.mxfcode.back.service.PermissionService;
import com.mxfcode.back.service.UserService;
import com.mxfcode.bbs_common.response.PageDataResult;
import com.mxfcode.bbs_common.util.StringUtils;
import com.mxfcode.entity.TSPermission;
import com.mxfcode.entity.TSUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @类名 UserController
 * @描述 用户控制器
 * @作者 mxf
 * @创建日期 2021-03-05 13:57
 **/
@Controller
@RequestMapping("/back/user")
@LoginRequired
public class UserController {
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/getUserList", method = RequestMethod.POST)
    @ResponseBody
    public PageDataResult getUserList(@RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize,UserSearchDTO userSearch) {
        /*logger.info("分页查询用户列表！搜索条件：userSearch：" + userSearch + ",pageNum:" + page.getPageNum()
                + ",每页记录数量pageSize:" + page.getPageSize());*/
        PageDataResult pdr = new PageDataResult();
        try {
            if(null == pageNum) {
                pageNum = 1;
            }
            if(null == pageSize) {
                pageSize = 10;
            }
            // 获取用户列表
            pdr = userService.getUserList(userSearch, pageNum ,pageSize);
            logger.info("用户列表查询=pdr:" + pdr);

        } catch (Exception e) {
            e.printStackTrace();
            logger.error("用户列表查询异常！", e);
        }
        return pdr;
    }

    @RequestMapping(value = "/setUser", method = RequestMethod.POST)
    @ResponseBody
    public Map<String,Object> setUser(TSUser user) throws Exception {
        logger.info("设置用户[新增或更新]！user:" + user);
        Map<String,Object> data = new HashMap();
        if(StringUtils.isBlank(user.getId())){
            data = userService.addUser(user);
        }else{
            data = userService.updateUser(user);
        }
        return data;
    }

    @RequestMapping(value = "/updateUserStatus", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> updateUserStatus(@RequestParam("id") String id,@RequestParam("status") Integer status) {
        logger.info("删除/恢复用户！id:" + id+" status:"+status);
        Map<String, Object> data = new HashMap<>();
        if(status == 0){
            //删除用户
            data = userService.delUser(id,status);
        }else{
            //恢复用户
            data = userService.recoverUser(id,status);
        }
        return data;
    }

    @PostMapping(value = "/updateUser")
    @ResponseBody
    public Map<String,Object> updateUser(TSUser user){
        logger.info("用户更新个人信息！id:" + user.getId());
        Map<String, Object> data = userService.update(user);
        return data;
    }
}
